/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Data.EnrollEvent;
import Data.Event;
import Data.Member;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author NGOCLINH-PC
 */
public class EnrollEventPR extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            if (request.getParameter("txtEenrolleventID") != null) {
                int id = Integer.parseInt(request.getParameter("txtEenrolleventID"));
                int txtIspayment = Integer.parseInt(request.getParameter("txtIspayment"));
                int stt = Integer.parseInt(request.getParameter("radio1"));
                String txtDatePay = request.getParameter("txtDatePay");
                EnrollEvent.Cupdate(id, txtIspayment, txtDatePay, stt);

                /// send mail
                EnrollEvent ct = new EnrollEvent();
                ArrayList<EnrollEvent> p = ct.SselectByID(id);
                if (p.size() > 0) {
                    for (int i = 0; i < p.size(); i++) {
                        EnrollEvent pay = p.get(i);

                        Member m = new Member();
                        ArrayList<Member> mb = m.MselectById(pay.getMemberID());
                        for (int u = 0; u < mb.size(); u++) {
                            Member ma = mb.get(u);
                            String email = ma.getEmail();

                            String subject = "Message from KSC";
                            String msg = "Dear " + ma.getMemberName() + "! \n"
                                    + "KSC has received fees for attending the event from you. Form registration your participation has been activated. \n"
                                    + "You can see here \n"
                                    + "Best Regards,\n"
                                    + "KSC";
                            Event.send(email, subject, msg, "t1106k.nnlinh@aptech.vn", "ngoclinh113");
                        }
                    }
                }

                response.sendRedirect("/nlpp/admin/index.jsp?page=EnrollEvent");

            } else if (request.getParameter(
                    "AenrolleventID") != null) {
                int idcate = Integer.parseInt(request.getParameter("AenrolleventID"));
                EnrollEvent.Cmtdelete(idcate);
                response.sendRedirect("/nlpp/admin/index.jsp?page=EnrollEvent");
            }

            if (request.getParameter("txtEventIDEnroll") != null && request.getParameter("txtMemEnroll") != null) {
                int eventID = Integer.parseInt(request.getParameter("txtEventIDEnroll"));
                int txtPay = Integer.parseInt(request.getParameter("txtPay"));
                int txtMemID = Integer.parseInt(request.getParameter("txtMemEnroll"));
                Date now = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dateRegis = sdf.format(now.getTime());
                EnrollEvent.EnInser(eventID, txtMemID, 0, dateRegis, "", txtPay, 1);
                out.print("<script>alert('We will confirm your registration form as soon as you pay the fee to us by the method that you have choose!');window.location.replace('client/index.jsp');</script>");
            } else {
                out.print("<script>alert('You must be Login before Enrollevent!');window.location.replace('client/index.jsp?page=Login');</script>");

            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
